package com.matt.controller;


import com.dotflat.fin.utils.excelUtil.ExcelUtil;
import com.matt.domain.AboutPallet;
import com.matt.foundation.excel.AboutPalletExcel;
import com.matt.foundation.model.StatusResponse;
import com.matt.foundation.query.AboutPalletQuery;
import com.matt.service.AboutPalletService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 预约试托控制层
 *
 * @author makejava
 * @since 2025-02-20 11:01:19
 */
@RestController
@RequestMapping("/aboutPallet")
@AllArgsConstructor
public class AboutPalletController {

	private AboutPalletService aboutPalletService;

	/**
	 * 获取预约试托列表(分页)
	 */
	@PostMapping("/list")
	public StatusResponse list(@RequestHeader String ticket, @RequestBody AboutPalletQuery aboutPallet) {
		return StatusResponse.success(aboutPalletService.list(ticket,aboutPallet));
	}


	@ApiOperation(value = "预约试托",httpMethod = "GET")
	@GetMapping("/derivePallet")
	public void derivePallet(HttpServletResponse response, AboutPalletQuery classesQuery,@RequestHeader String ticket) throws IOException {
		classesQuery.setPageNum(1);
		classesQuery.setPageSize(1000);
		Page<AboutPallet> aboutPallets = aboutPalletService.list(ticket,classesQuery);
		List<AboutPalletExcel> aboutPalletExcels = new ArrayList<>();
		if(!aboutPallets.getContent().isEmpty()){
			aboutPallets.forEach(a->{
				AboutPalletExcel palletExcel = new AboutPalletExcel();
				BeanUtils.copyProperties(a,palletExcel);
				switch(a.getStatus()){
					case 1 : palletExcel.setStatus("已试托");break;
					case 2 : palletExcel.setStatus("未试托");break;
					default : palletExcel.setStatus("_");
				}

				aboutPalletExcels.add(palletExcel);
			});
		}
		ExcelUtil<AboutPalletExcel> excelUtil = new ExcelUtil<AboutPalletExcel>(AboutPalletExcel.class);
		excelUtil.exportExcel(response,aboutPalletExcels,"预约试托导出","预约试托导出");
	}

	@PostMapping("/getList")
	public StatusResponse getList(@RequestBody AboutPalletQuery aboutPallet) {
		return StatusResponse.success(aboutPalletService.getList(aboutPallet));
	}


	/**
	 * 获取预约试托
	 */
	@GetMapping("/get")
	public StatusResponse get(String id) {
		return StatusResponse.success(aboutPalletService.findById(id));
	}

	/**
	 * 添加预约试托
	 */
	@PostMapping("/add")
	public StatusResponse add(@RequestHeader String ticket,@RequestBody AboutPallet aboutPallet) {
		return StatusResponse.success(aboutPalletService.save(ticket,aboutPallet));
	}


	/**
	 * 添加预约到访
	 */
	@PostMapping("/wpAdd")
	public StatusResponse wpAdd(@RequestBody AboutPallet aboutPallet) {

		return StatusResponse.success(aboutPalletService.wpAdd(aboutPallet));
	}

	/**
	 * 修改预约试托
	 */
	@PostMapping("/update")
	public StatusResponse update(@RequestHeader String ticket,@RequestBody AboutPallet aboutPallet) {
		return StatusResponse.success(aboutPalletService.update(ticket,aboutPallet));
	}

	@PostMapping("/updateLeads")
	public StatusResponse updateLeads(@RequestHeader String ticket,@RequestBody AboutPallet aboutPallet) {
		return aboutPalletService.updateLeads(ticket,aboutPallet);
	}


	/**
	 * 删除预约试托
	 */
	@PostMapping("/delete")
	public void delete(String id) {
		aboutPalletService.deleteById(id);
	}

}

